Skip to content

Fix the information_schema queries.#37

Open
tekkamanendless wants to merge 3 commits intogo-gorm:masterfrom
tekkamanendless:fix-information-schema-queries
Open

Fix the information_schema queries.#37
tekkamanendless wants to merge 3 commits intogo-gorm:masterfrom
tekkamanendless:fix-information-schema-queries

Conversation

@tekkamanendless
Copy link

BigQuery requires that information_schema queries include the dataset name. This fixes the various Migrator methods to do this.

This fixes #17.

BigQuery requires that `information_schema` queries include the
dataset name.  This fixes the various Migrator methods to do this.
@jinzhu
Copy link
Member

jinzhu commented Jan 31, 2026

HI @tekkamanendless

Could we avoid making BigQueryConnection a public struct?

@tekkamanendless
Copy link
Author

tekkamanendless commented Jan 31, 2026

@jinzhu unfortunately, we need to be able to get the dataset from the connection in the main package in migrator.go, which we get from the connection string.

For example, we use it with HasTable:

func (m Migrator) HasTable(value interface{}) bool {
	var count int64
	m.RunWithValue(value, func(stmt *gorm.Statement) error {
		// According to the BigQuery documentation, an INFORMATION_SCHEMA view must be qualified with a dataset or region.
		// See: https://docs.cloud.google.com/bigquery/docs/information-schema-intro
		//
		// We are going to attempt to get the dataset ID from the connection and use it to qualify the INFORMATION_SCHEMA view.
		datasetID, err := m.getDatasetID()
		if err != nil {
			return err
		}
		return m.DB.Raw("SELECT count(*) FROM `"+datasetID+".INFORMATION_SCHEMA.TABLES` WHERE table_name = ?", stmt.Table).Row().Scan(&count)
	})

	return count > 0
}
func (m Migrator) CurrentDatabase() (name string) {
	datasetID, err := m.getDatasetID()
[...]
	return datasetID
}
// getDatasetID is a helper function to get the dataset ID from the connection.
func (m Migrator) getDatasetID() (string, error) {
[...]
		dataset := bigQueryConnection.GetDataset()
[...]
	return datasetID, nil
}

We could potentially expose a public interface that has GetDataset available instead of the whole structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrator show table not exist but Automigrate fail as table exists

3 participants